package com.BMS.DAOImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.ParameterMode;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import com.BMS.DAO.BookDao;
import com.BMS.entity.Book;

/**
 * 图书实体类DAO层接口实现类
 * 
 * @author lenovo
 *
 */
@Repository("BookDaoImpl")
public class BookDaoImpl implements BookDao {

	@Resource
	private SessionFactory sessionFactory;

	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	// add
	@Override
	public void addBook(Book book, String manager_id, Date takeOn_date) {
		// 调用图书上架存储过程
		ProcedureCall procedure = this.getSession().createStoredProcedureCall("takeOnBook");
		procedure.registerParameter(0, String.class, ParameterMode.IN).bindValue(book.getBook_ID());
		procedure.registerParameter(1, String.class, ParameterMode.IN).bindValue(book.getTitle());
		procedure.registerParameter(2, String.class, ParameterMode.IN).bindValue(book.getType());
		procedure.registerParameter(3, String.class, ParameterMode.IN).bindValue(book.getAuthor());
		procedure.registerParameter(4, String.class, ParameterMode.IN).bindValue(book.getPublisher());
		procedure.registerParameter(5, Date.class, ParameterMode.IN).bindValue(book.getPublish_date());
		procedure.registerParameter(6, Double.class, ParameterMode.IN).bindValue(book.getPrice());
		procedure.registerParameter(7, String.class, ParameterMode.IN).bindValue(manager_id);
		procedure.registerParameter(8, Date.class, ParameterMode.IN).bindValue(takeOn_date);
		
		procedure.execute();
	}

	// delete
	@Override
	public void deleteBookById(String book_id) {
		this.getSession().createQuery("delete Book where book_ID=?").setParameter(0, book_id).executeUpdate();
	}

	// takeOff
	// @param book_id, manager_id, takeOff_date
	public void takeOffBook(String book_id, String manager_id, Date takeOff_date) {		
		ProcedureCall procedure = this.getSession().createStoredProcedureCall("takeOffBook");
		procedure.registerParameter(0, String.class, ParameterMode.IN).bindValue(book_id);
		procedure.registerParameter(0, String.class, ParameterMode.IN).bindValue(manager_id);
		procedure.registerParameter(0, Date.class, ParameterMode.IN).bindValue(takeOff_date);
		
		procedure.execute();
	}

	// find
	@Override
	public Book findBookById(String book_id) {
		return (Book) this.getSession().createQuery("from Book where book_ID=?").setParameter(0, book_id)
				.uniqueResult();
	}

	// 分页查询图书，并按指定字段排序
	public List<Book> findAllBooks(int offset, int length, String orderBy) {

		List<Book> bookList = null;
		Session session = this.getSession();
		Query query = session.createQuery("from Book b order by ? desc").setParameter(0, "b." + orderBy);
		query.setFirstResult(offset);
		query.setMaxResults(length);
		bookList = query.list();
		// System.out.println(orderBy);
		return bookList;
	}

	// update
	@Override
	public void updateBook(Book book) {
		this.getSession().update(book);
	}

	// 显示分页总页数
	public long getPageCount() {
		Query query = this.getSession().createQuery("SELECT COUNT(*) FROM Book");
		// 每页10行记录
		return (long) Math.ceil(Double.parseDouble(query.uniqueResult().toString()) / 10);
	}

}
